package com.ruoyi.shopping.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.shopping.domain.CommodityCollect;
import com.ruoyi.shopping.service.ICommodityCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品收藏管理Controller
 *
 * @author fxy
 * @date 2024-08-14
 */
@RestController
@RequestMapping("/shopping/commodityCollect")
public class CommodityCollectController extends BaseController {
    @Autowired
    private ICommodityCollectService commodityCollectService;

    /**
     * 查询商品收藏管理列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityCollect:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommodityCollect commodityCollect) {
        startPage();
        List<CommodityCollect> list = commodityCollectService.selectCommodityCollectList(commodityCollect);
        return getDataTable(list);
    }

    /**
     * 导出商品收藏管理列表
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityCollect:export')")
    @Log(title = "商品收藏管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommodityCollect commodityCollect) {
        List<CommodityCollect> list = commodityCollectService.selectCommodityCollectList(commodityCollect);
        ExcelUtil<CommodityCollect> util = new ExcelUtil<CommodityCollect>(CommodityCollect.class);
        util.exportExcel(response, list, "商品收藏管理数据");
    }

    /**
     * 获取商品收藏管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityCollect:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(commodityCollectService.selectCommodityCollectById(id));
    }

    /**
     * 新增商品收藏管理
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityCollect:add')")
    @Log(title = "商品收藏管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommodityCollect commodityCollect) {
        return toAjax(commodityCollectService.insertCommodityCollect(commodityCollect));
    }
    /* public int deleteCommodityCollect(CommodityCollect commodityCollect);
    //查询商品是否被用户收藏
    CommodityCollect queryByCCId(CommodityCollect goodsCollect);*/
    //取消收藏
    @PreAuthorize("@ss.hasPermi('shopping:commodityCollect:remove')")
    @Log(title = "商品收藏管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/CId")
    public AjaxResult removeCollection(@RequestBody CommodityCollect commodityCollect) {
        return toAjax(commodityCollectService.deleteCommodityCollect(commodityCollect));
    }
    //查询商品是否被收藏
    @PreAuthorize("@ss.hasPermi('shopping:commodityCollect:query')")
    @GetMapping(value = "/isCollection")
    public AjaxResult isCollection(CommodityCollect commodityCollect) {
        return success(commodityCollectService.queryByCCId(commodityCollect));
    }


    /**
     * 修改商品收藏管理
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityCollect:edit')")
    @Log(title = "商品收藏管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommodityCollect commodityCollect) {
        return toAjax(commodityCollectService.updateCommodityCollect(commodityCollect));
    }

    /**
     * 删除商品收藏管理
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityCollect:remove')")
    @Log(title = "商品收藏管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(commodityCollectService.deleteCommodityCollectByIds(ids));
    }
    @PreAuthorize("@ss.hasPermi('shopping:commodityCollect:removeByCCId')")
    @Log(title = "商品收藏管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/byCCId")
    public AjaxResult removeByCargoIdAndCommodityId(
            @RequestParam Long cargoUserId,
            @RequestParam Long commodityId) {
        System.out.println("cargoUserId: " + cargoUserId);
        System.out.println("commodityId: " + commodityId);
        return toAjax(commodityCollectService.deleteCommodityCollectByCargoIdAndCommodityId(cargoUserId, commodityId));
    }
    /**
     * 获取我的商品收藏
     */
    @PreAuthorize("@ss.hasPermi('shopping:commodityCollect:query')")
    @GetMapping("/getCollect")
    public AjaxResult getCollect(Long cargoId) {
        System.out.println(cargoId+"asdsa");
        return success(commodityCollectService.selectCommodityCollectsBycargoUserId(cargoId));
    }
}
